<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$servername = "localhost";
$username = "root"; // 数据库用户名
$password = "store"; // 数据库密码
$dbname = "aiweb"; // 数据库名称

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接
if ($conn->connect_error) {
    echo json_encode(['success' => false, 'message' => '连接失败: ' . $conn->connect_error]);
    exit;
}

// 获取搜索关键字
$search = $_POST['search'] ?? ''; // 获取传递的搜索词

// 查询数据库
if (!empty($search)) {
    $sql = "SELECT * FROM app WHERE appname LIKE ? OR info LIKE ?";
    $stmt = $conn->prepare($sql);
    $likeSearch = "%" . $search . "%";
    $stmt->bind_param("ss", $likeSearch, $likeSearch);
    $stmt->execute();
    $result = $stmt->get_result();

    $apps = [];
    while ($row = $result->fetch_assoc()) {
        $apps[] = $row;
    }
    $stmt->close();

    // 返回结果
    echo json_encode(['success' => true, 'data' => $apps]);
} else {
    // 如果没有搜索词，返回空数组
    echo json_encode(['success' => true, 'data' => []]);
}

$conn->close();
?>
